package net.viralpatel.struts2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ProfileAction extends ActionSupport{
	private User user;
	String url = "jdbc:mysql://localhost:3306/";
	String dbName = "t10";
	String driverName = "com.mysql.jdbc.Driver";
	Connection con = null;
	ResultSet resultSet = null;
	String nameroot = "root";
	String passwordroot = "";
	public String execute()throws Exception{
		try {
			Class.forName(driverName);
			con = DriverManager.getConnection(url + dbName,nameroot,passwordroot);
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());	
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		
		String selectLogin = "SELECT * FROM user WHERE userID = ?";
		PreparedStatement prepStmt = con.prepareStatement(selectLogin);
		Map session=ActionContext.getContext().getSession();
		prepStmt.setString(1, session.get("username").toString());
		resultSet = prepStmt.executeQuery();
		resultSet.first();
		user = new User();
		user.setUsername(resultSet.getString(1));
		user.setName(resultSet.getString(3));
		user.setEmail(resultSet.getString(4));
		user.setTelephone(resultSet.getString(5));
		user.setCompany(resultSet.getString(6));
		return SUCCESS;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

}
